//
//  ConNewClubCardVc.m
//  Shengzhuli
//
//  Created by FXGuo on 2018/1/12.
//  Copyright © 2018年 kamangkeji. All rights reserved.
//

#import "ConNewClubCardVc.h"
#import "YYLabel.h"
#import "NSAttributedString+YYText.h"
#import "MBProgressHUD+MJ.h"
#import <UIImageView+WebCache.h>
#import "ShareTool.h"
#import "QRCodeCreateTool.h"
#import "UIImage+Extension.h"

@interface ConNewClubCardVc ()
@property (weak, nonatomic) IBOutlet UIView *shareView;
@property (weak, nonatomic) IBOutlet UIImageView *bgImgView;

@property (weak, nonatomic) IBOutlet UIView *iconConView;
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet YYLabel *introLab;
@property (weak, nonatomic) IBOutlet UILabel *clubNameLab;
@property (weak, nonatomic) IBOutlet UILabel *subTitleLab;
@property (weak, nonatomic) IBOutlet UIImageView *codeImgView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bgimgHCons;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *codeImgWCons;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *codeImgHCons;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *iconWCons;


@end

@implementation ConNewClubCardVc

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"俱乐部名片";
    self.view.backgroundColor = RGBColorOne(102);
    if (IS_IPHONE_5) {
        self.bgimgHCons.constant = (ScreenW-40)*100/279;
        self.codeImgWCons.constant = 40;
        self.codeImgHCons.constant = 40;
        self.clubNameLab.font = Font(17);
        self.subTitleLab.font = Font(12);
        self.iconWCons.constant = 70;
        self.iconView.layer.cornerRadius = 35;
        self.iconConView.layer.cornerRadius = 38;
    }else {
        self.bgimgHCons.constant = (ScreenW-40)*120/279;
    }
    if (IS_IPHONE_X) {
        self.iconWCons.constant = 110;
    }
    [self setNavi];
    [self setConfig];
}
- (void)setConfig
{
    [self.bgImgView sd_setImageWithURL:[NSURL URLWithString:self.model.backgroundImg] placeholderImage:GetImage(@"club_bg")];
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:self.model.clubLogo] placeholderImage:GetImage(@"club_icon")];
    self.clubNameLab.text = self.model.clubName;
    self.subTitleLab.text = [NSString stringWithFormat:@"发布过%@个活动,累计参加人数%@人",self.model.activityCount,self.model.activityPersonCount];
    
    
    NSString *str = self.model.content;
    self.introLab.numberOfLines = 0;
    self.introLab.preferredMaxLayoutWidth = ScreenW-80;
    
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:str];
    attri.yy_font = Font(17);
    if (IS_IPHONE_5) {
        attri.yy_font = Font(14);
    }
    attri.yy_color = RGBColorOne(85);
    attri.yy_alignment = NSTextAlignmentJustified;
    attri.yy_lineSpacing = 4;
    attri.yy_kern = @(0);

    //计算文本尺寸
    CGSize maxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 80, MAXFLOAT);
    YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:maxSize text:attri];
    self.introLab.textLayout = layout;
    self.introLab.attributedText = attri;
    
    UIImage *codeImg = [QRCodeCreateTool createQRCodeWithString:self.model.clubUrl];
    self.codeImgView.image = codeImg;
}
- (IBAction)shareBtnClick:(UIButton *)btn
{
    UIImage *shareImg = [UIImage snapshotView:self.view fromRect:CGRectMake(self.shareView.x-15, self.shareView.y-15, self.shareView.width+30, self.shareView.height+30) withCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    if (btn.tag == 2) {
        [self loadImageFinished:shareImg];
    }else {
        [ShareTool shareWithImg:shareImg type:btn.tag];
    }
}


- (void)setNavi
{
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:18]}];
    self.navigationController.navigationBar.barTintColor = RGBColorOne(102);
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
}

- (UIBarButtonItem *)customBackItemWithTarget:(id)target action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"ic_left_back_select"] forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, 40, 40);
    [button sizeToFit];
    [button addTarget:target
               action:action
     forControlEvents:UIControlEventTouchUpInside];
    return [[UIBarButtonItem alloc] initWithCustomView:button];
}

//保存图片到相册
- (void)loadImageFinished:(UIImage *)image
{
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    [MBProgressHUD showOnlyText:@"保存图片成功,请去相册查看" view:self.view];
    NSLog(@"image = %@, error = %@, contextInfo = %@", image, error, contextInfo);
}


@end
